﻿using System;
using System.Text;
using BloggerAutoPoster.Common.CodePlexRss.Model;
using System.Threading;
using BloggerAutoPoster.Common.Mvvm;
using System.IO;
using System.Windows.Forms;
using BloggerAutoPosterService.Engine.Utils;
using BloggerAutoPoster.Common.CodePlexRss;
using System.Net;
using BloggerAutoPosterService.Engine.Core;

namespace BloggerAutoPoster.Infrastructure.ViewModels
{
    public class AboutViewModel : ViewModelBase
    {
        #region Fields (3)

        bool _isBusy;
        readonly ServiceConfig _serviceConfig = new ServiceConfig(true);
        VersionsInfo _versionsInfoData;

        #endregion Fields

        #region Constructors (1)

        public AboutViewModel()
        {
            VersionsInfoData = new VersionsInfo();
            new Thread(downloadInfo).Start();
        }

        #endregion Constructors

        #region Properties (2)

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                if (_isBusy == value) return;
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        public VersionsInfo VersionsInfoData
        {
            set
            {
                _versionsInfoData = value;
                RaisePropertyChanged("VersionsInfoData");
            }
            get { return _versionsInfoData; }
        }

        #endregion Properties

        #region Methods (3)

        // Private Methods (3) 

        private static string cacheManager(string rssXml)
        {
            string localCacheFile = Path.Combine(Application.StartupPath, "rss.xml");

            if (string.IsNullOrWhiteSpace(rssXml))
            {
                //try using a local cache
                if (File.Exists(localCacheFile))
                    rssXml = File.ReadAllText(localCacheFile);
            }
            else
            {
                //cache it
                File.WriteAllText(localCacheFile, rssXml, Encoding.UTF8);
            }

            return rssXml;
        }

        void downloadInfo()
        {
            try
            {
                IsBusy = true;

                string rssXml = tryDownloadRss();
                rssXml = cacheManager(rssXml);

                if (string.IsNullOrWhiteSpace(rssXml))
                {
                    IsBusy = false;
                    return;
                }

                VersionsInfoData.Clear();
                VersionsInfoData = DownloadHistory.ParseInfo(rssXml);
            }
            catch (Exception ex)
            {
                new Logger(_serviceConfig).WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }

        private string tryDownloadRss()
        {
            var rssXml = string.Empty;
            try
            {
                var url = _serviceConfig.ProjectRSSFeed;
                if (string.IsNullOrWhiteSpace(url)) return string.Empty;
                rssXml = new WebClient { Encoding = Encoding.UTF8 }.DownloadString(url);
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("The remote name could not be resolved"))
                {
                    new Logger(_serviceConfig).WriteLine(ex);
                }
            }
            return rssXml;
        }

        #endregion Methods
    }
}
